package com.jeesite.modules.exam.assessment.service;

import java.util.Date;
import java.util.List;

import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.exam.notice.entity.DcSupExamNoticeOrg;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.utils.OfficeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.exam.assessment.entity.DcSupExamInfo;
import com.jeesite.modules.exam.assessment.dao.DcSupExamInfoDao;
import com.jeesite.modules.file.utils.FileUploadUtils;
import org.springframework.util.CollectionUtils;

/**
 * 考核任务信息表Service
 * @author xhc
 * @version 2022-05-28
 */
@Service
@Transactional(readOnly=false)
public class DcSupExamInfoService extends CrudService<DcSupExamInfoDao, DcSupExamInfo> {
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	/**
	 * 获取单条数据
	 * @param dcSupExamInfo
	 * @return
	 */
	@Override
	public DcSupExamInfo get(DcSupExamInfo dcSupExamInfo) {
		return super.get(dcSupExamInfo);
	}
	
	/**
	 * 查询分页数据
	 * @param dcSupExamInfo 查询条件
	 * @param dcSupExamInfo.page 分页对象
	 * @return
	 */
	@Override
	public Page<DcSupExamInfo> findPage(DcSupExamInfo dcSupExamInfo) {
		return super.findPage(dcSupExamInfo);
	}
	
	/**
	 * 查询列表数据
	 * @param dcSupExamInfo
	 * @return
	 */
	@Override
	public List<DcSupExamInfo> findList(DcSupExamInfo dcSupExamInfo) {
		//当前单位
//		Office unitOffice  = sysOfficeUnitService.getLatestUnitByOffice();
//		dcSupExamInfo.setCreateUnit(unitOffice.getOfficeCode());
//		dcSupExamInfo.setCreateUnitName(unitOffice.getOfficeName());
		return super.findList(dcSupExamInfo);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param dcSupExamInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DcSupExamInfo dcSupExamInfo) {
		//当前单位或部门
		Office currentOffice = OfficeUtil.getCurrentOffice();
		//当前单位
		Office unitOffice  = sysOfficeUnitService.getLatestUnitByOffice();
		dcSupExamInfo.setCreateOrg(currentOffice.getOfficeCode());
		dcSupExamInfo.setCreateOrgName(currentOffice.getOfficeName());
		dcSupExamInfo.setCreateUnit(unitOffice.getOfficeCode());
		dcSupExamInfo.setCreateUnitName(unitOffice.getOfficeName());
		super.save(dcSupExamInfo);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(dcSupExamInfo, dcSupExamInfo.getId(), "dcSupExamInfo_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(dcSupExamInfo, dcSupExamInfo.getId(), "dcSupExamInfo_file");
	}
	
	/**
	 * 更新状态
	 * @param dcSupExamInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(DcSupExamInfo dcSupExamInfo) {
		super.updateStatus(dcSupExamInfo);
	}
	
	/**
	 * 删除数据
	 * @param dcSupExamInfo
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcSupExamInfo dcSupExamInfo) {
		super.delete(dcSupExamInfo);
	}

	/**
	 * 任务发布
	 * @param dcSupExamInfo
	 */
	@Transactional(readOnly=false)
	public void publish(DcSupExamInfo dcSupExamInfo) {
		dao.publish(dcSupExamInfo.getId(),dcSupExamInfo.getExamStatus());
	}

	/**
	 * 撤销任务
	 * @param dcSupExamInfo
	 * return
	 */
	@Transactional(readOnly=false)
	public String undo(DcSupExamInfo dcSupExamInfo) {
		// 查询 考核任务通知表-被通知单位 是否已经有单位接收通知
	 List<DcSupExamNoticeOrg> noticeOrgs=dao.noticeById(dcSupExamInfo.getId(),"2");
	 if (CollectionUtils.isEmpty(noticeOrgs)){
		 dao.publish(dcSupExamInfo.getId(),"1");
		 List<DcSupExamNoticeOrg> noticeOrgs_=dao.noticeById(dcSupExamInfo.getId(),"1");
		 noticeOrgs_.forEach(
				 x->{dao.deleteNoticeOrg(x);}
		 );
		 dao.deleteNotice(dcSupExamInfo);

		 return "success";
	 }
	     return "error";
	}

	public String getVersion(DcSupExamInfo dcSupExamInfo) {
		return dao.getVersion(dcSupExamInfo);
	}
	@Transactional(readOnly=false)
    public void saveRole(EmpUser empUser) {
		dao.deleteUerRole(empUser);
		empUser.getUserRoleList().forEach(x->x.setUserCode(empUser.getUserCode()));
		dao.inserBatchUerRole(empUser.getUserRoleList());
    }

    public List<DcSupExamInfo> findQuotaList(DcSupExamInfo dcSupExamInfo) {
		return dao.findQuotaList(dcSupExamInfo);
    }

    public Page<EmpUser> findPageCopy(EmpUser empUser) {
		Page page = empUser.getPage();
		List list = dao.findPageCopyList(empUser);
		page.setList(list);
		return page;
    }
}